package cn.bouncyslime.graduationProject.nba.service;

import cn.bouncyslime.graduationProject.nba.crawler.spider.CompetitionPageProcessor;
import cn.bouncyslime.graduationProject.nba.dao.CompetitionDao;
import cn.bouncyslime.graduationProject.nba.dao.SeasonDao;
import cn.bouncyslime.graduationProject.nba.util.BigDecimalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 张一卓
 * @date 2020/5/7 16:00
 **/
@Service
public class CompetitionService {

    private static final Logger logger = LoggerFactory.getLogger(CompetitionService.class);

    @Autowired
    CompetitionDao competitionDao;
    @Autowired
    SeasonDao seasonDao;

    public List<Map<String, Object>> listEastTeamGradeCurrentSeason(){
        String currentSeason = seasonDao.getCurrentSeason();
        List<Map<String, Object>> winsList = competitionDao.listEastTeamWins(currentSeason);
        List<Map<String, Object>> lossesList = competitionDao.listEastTeamLosses(currentSeason);
        List<Map<String, Object>> result = new ArrayList<>(winsList.size());
        for (int i=0; i<winsList.size(); i++){
            Map<String, Object> winsMap = winsList.get(i);
            int wins = Integer.parseInt(winsMap.get("wins").toString());
            int losses = 0;
            for (int j = 0; j<lossesList.size(); j++){
                Map<String, Object> lossesMap = lossesList.get(j);
                if (lossesMap.get("teamId").equals(winsMap.get("teamId"))){
                    losses = Integer.parseInt(lossesList.get(j).get("losses").toString());
                    break;
                }
            }
            winsMap.put("losses", losses);
            winsMap.put("winRate", BigDecimalUtil.divide(wins, wins + losses, 3));
            result.add(winsMap);
        }
//        logger.debug(result.toString());
        return result;
    }

    public List<Map<String, Object>> listWestTeamGradeCurrentSeason(){
        String currentSeason = seasonDao.getCurrentSeason();
        List<Map<String, Object>> winsList = competitionDao.listWestTeamWins(currentSeason);
        List<Map<String, Object>> lossesList = competitionDao.listWestTeamLosses(currentSeason);
        List<Map<String, Object>> result = new ArrayList<>(winsList.size());
        for (int i=0; i<winsList.size(); i++){
            Map<String, Object> winsMap = winsList.get(i);
            int wins = Integer.parseInt(winsMap.get("wins").toString());
            int losses = 0;
            for (int j = 0; j<lossesList.size(); j++){
                Map<String, Object> lossesMap = lossesList.get(j);
                if (lossesMap.get("teamId").equals(winsMap.get("teamId"))){
                    losses = Integer.parseInt(lossesList.get(j).get("losses").toString());
                    break;
                }
            }
            winsMap.put("losses", losses);
            winsMap.put("winRate", BigDecimalUtil.divide(wins, wins + losses, 3));
            result.add(winsMap);
        }
//        logger.debug(result.toString());
        return result;
    }
}
